<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Storage;
class ImageController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {   $image = DB::table('image')->get();
        return view('home.image_index')->with('img',$image);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        // return view('home.image_add');
        return view('home.ajax_image');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        
/*        //方一
        // dd($request->file('img'));
        $image=$request->file('img');
        $urls=[];
        foreach ($image as $key => $val) {
            if($val->isValid()){
                $ext = $val->getClientOriginalExtension();//取后缀名
                $path = 'upload/image/';
                $name = time().rand(1,1000).'.'.$ext;
                if($val->move($path,$name)){
                    $url = '/upload/image/'.$name;
                    $urls[]=['url'=>$url];  
                }
            }
        }
         // dd($urls);
        DB::table('image')->insert($urls);
        return redirect('image');*/

        /*方二.控制器*/
        $image=$request->file('img');
        $urls=[];
        foreach ($image as $key => $val) {
            if($val->isValid()){
                $ext = $val->getClientOriginalExtension();//取后缀名
                $path = 'image/';
                $name = time().rand(1,1000).'.'.$ext;
                $path.=$name;
                //获取文件临时路径
                $temp_path = $val->getRealPath();
                //利用put执行上传文件
                Storage::disk('imgup')->put($path,file_get_contents($temp_path));
                $urls[]=['url'=>'/upload/'.$path];
            }
        }
         // dd($urls);
        DB::table('image')->insert($urls);
        // return redirect('image');
        $data=[
            'status'=>200,
            'msg'=>'成功',
            'data'=>$urls
        ];
        echo json_encode($data);

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
